<?php

namespace IDock\Insurance\JTIC\Response;

use IDock\Http\Response;
use IDock\Insurance\JTIC\Response\AllJTICResponse;
use IDock\Printer\NonCar\NonCarProductListPrinter;
use IDock\Printer\NonCar\NonCarProductPrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Printer\UnderwritingRecallPrinter;

class NonCarProductListResponse extends AllJTICResponse
{

    public function printerInit(): Response
    {
        $body = $this->getBody();
        $proList = $body['ProjectACCList']['ProjectItem'] ?? [];
        $proList = $this->formatArray($proList);
        $printer = new NonCarProductListPrinter();

        foreach ($proList as $item) {
            $productPrinter = new NonCarProductPrinter();
            $productPrinter->productCode = $item['ProjectCode'] ?? '';//产品编码
            $productPrinter->productName = $item['ProjectName'] ?? '';//产品名称
            $productPrinter->unitAmount = $item['ProjectAmount'] ?? '';//保额
            $productPrinter->productType = '驾意险';
            $productPrinter->productDesc = $item['SpecContent'] ?? '';
            $productPrinter->premium = $item['ProjectPremium'] ?? '';
            $productPrinter->planCode = $item['ProjectCode'] ?? '';//方案代码
            $productPrinter->planName = $item['ProjectName'] ?? '';//方案名称
            $productPrinter->argc['ProjectType'] = $item['ProjectType'];

            $planList  = $this->formatArray($item['PlanList'] ?? []);

            foreach ($planList as $planItem) {

                $planItem  = $this->formatArray($planItem['planItem'] ?? []);

                foreach ($planItem as $planValue) {
                    $planName = $planValue['PlanName'] ?? '';

                    foreach ($planValue['CoverageList']['CoverageItem'] ?? [] as $CoverageItem) {
                        $PlanName = $planItem['PlanName'] ?? '';


                        $subCoverage = new NonCarSubCoveragePrinter();
                        $subCoverage->subCode = $CoverageItem['CoverageCode'] ?? '';
                        $subCoverage->subName = $planName.($CoverageItem['CoverageName'] ?? '');
                        $subCoverage->amount = $CoverageItem['CvrgLiabAmount'] ?? '';
                        $productPrinter->subCoverageAppend($subCoverage);
                    }
                }




            }

            //$coverageList = $this->formatArray($item['CoverageList']['CoverageItem'] ?? []);
            //
            //foreach ($coverageList as $sub) {
            //    $subCoverage = new NonCarSubCoveragePrinter();
            //    $subCoverage->subCode = $sub['CoverageCode'] ?? '';
            //    $subCoverage->subName = $sub['LiabilityName'] ?? '';
            //    $subCoverage->amount = $sub['PlanCoverageAmount'] ?? '';
            //    $productPrinter->subCoverageAppend($subCoverage);
            //}

            $printer->productListAppend($productPrinter);
        }


        $this->printer = $printer;
        return $this;
    }

    public function setSeats($seats) {
        if ($this->printer->productList ?? []) {

            /*** @var $value NonCarProductPrinter */
            foreach ($this->printer->productList as &$value) {
                //if ($value['argc']['ProjectType'] == 2) {
                //    $value['premium'] = round($seats* $value['premium']);
                //    $value['premium'] = number_format((float)$value['premium'], 2, '.', '');
                //}
            }

            usort($this->printer->productList,function ($a,$b) {
                return $a['premium'] > $b['premium'];
            });
        }
    }
}
